//index.js
const app = getApp()

Page({
  data: {
    inetrvalBtn: null,
    inetrvalBall: null,
    inetrvalPig: null,
    model: 'iphone 6',
    multClickF: true, // 防止多次点击
    pigImg: {
      top: 480
    },
    staticImgUrl: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    shareData: null
  },
  onLoad: function (options) {
    this.setData({
      model: app.globalData.model,
      system: app.globalData.systemInfo,
      screenHeight: app.globalData.screenHeight,
      staticImgUrl: app.globalData.staticImgUrl,
      shareData: app.globalData.shareData
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onReady() {
    this.setImgPosition()
    this.animationBtn = wx.createAnimation()
    this.animationBall = wx.createAnimation()
    this.animationPig = wx.createAnimation()
    this.startAnimations()
  },
  onGotUserInfo(e) {
    console.log(e.detail)
    if (e.detail.errMsg.indexOf('fail auth deny') >= 0){
      wx.showModal({
        title: '提示',
        content: '授权用户信息才能使用哦',
        showCancel: false,
        confirmText: '我知道了',
        confirmColor: '#EE7942'
      })
    } else if (e.detail.errMsg.indexOf('ok') >= 0) {
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },
  setImgPosition() {
    if (app.globalData.model.indexOf('iPhone 5') >= 0){
      this.setData({
        pigImg: {
          top: 455
        }
      })
    }
  },
  startAnimations() {
    // 抽签按钮动画
    this.data.inetrvalBtn = setInterval(() => {
      this.animationBtn.scale(1.1).step({
        duration: 500
      })
      this.animationBtn.scale(0.9).step({
        duration: 500
      })
      this.setData({ animationBtn: this.animationBtn.export() })
    }, 900)

    // 气球动画
    this.data.inetrvalBall = setInterval(() => {
      this.animationBall.translateY(5).step({
        duration: 1000
      })
      this.animationBall.translateY(-5).step({
        duration: 1000
      })
      this.setData({ animationBall: this.animationBall.export() })
    }, 1900)

    // 小猪动画
    this.animationPig.translate(5, 3).step({
      duration: 2000
    })
    this.animationPig.translate(-70, -50).step({
      duration: 0
    })
    this.setData({ animationPig: this.animationPig.export() })
    this.data.inetrvalPig = setInterval(() => { 
      this.animationPig.translate(5, 3).step({
        duration: 2000
      })
      this.animationPig.translate(-70, -50).step({
        duration: 0
      })
      this.setData({ animationPig: this.animationPig.export() })
    }, 2100)
  },
  toDrawTypePage(event) {
    console.log(this.data.multClickF)
    if (this.data.multClickF) {
      this.data.multClickF = false
      wx.navigateTo({
        url: '../draw-type/draw-type'
      })
    }
  },
  onHide() {
    this.data.multClickF = true
  },
  onShareAppMessage(url) {
    let shareData = this.data.shareData[Math.floor(Math.random() * 4)]
    return {
      title: shareData.text,
      imageUrl: shareData.imgUrl,
      path: 'pages/index/index'
    }
  }
})
